
#include "device/dev_manager.h"
#include "json/json-usr.h"
#include "route/route.h"
#include "socket/server.h"
#include "data/data.h"
#include "trans/trans.h"


using namespace iecas_ns;

shared_ptr<CDevManager> devManagerPtr;
shared_ptr<CJsonUsr> jsonUsrPtr;
shared_ptr<CServer> serverPtr;
shared_ptr<CData> dataPtr;
shared_ptr<CTrans> transPtr;


int main()
{
    plog::init(plog::debug, "log.txt", 1 * 1000 * 1000, 10);
    cout << "!Gateway start!" << endl;
    PLOGD << "!Gateway start!";

    shared_ptr<CCommon> commonPtr;
    commonPtr = CCommon::getInstance();
    
    jsonUsrPtr = CJsonUsr::getInstance();
    if(jsonUsrPtr->init())
    {
        jsonUsrPtr->start();
    }
    else
    {
        cout << "jsonUsrPtr init error" << endl;
        PLOGD << "jsonUsrPtr init error";
        return -1;
    }
    
    Value allParams, shParams;
    jsonUsrPtr->getAllParams(allParams);
    if(allParams.isMember("default_shell"))
    {
        shParams = allParams["default_shell"];
        for(int i = 0; i < shParams.size(); i++)
        {
            string cmd_shell;
            cmd_shell = "sh $PWD/" + shParams[i]["file_name"].asString();
            cout << "cpp call linux shell: " << cmd_shell << endl;
            PLOGD << "cpp call linux shell: " << cmd_shell;
            system(cmd_shell.c_str());
        }
    }
    else
    {
        cout << "get .json default_shell invalid" << endl;
        PLOGD << "get .json default_shell invalid";
    }

    devManagerPtr = CDevManager::getInstance();
    if(devManagerPtr->init())
    {
        devManagerPtr->start();
    }
    else
    {
        cout << "devManagerPtr init error" << endl;
        PLOGD << "devManagerPtr init error";
        return -1;
    }

    serverPtr = CServer::getInstance();
    if(serverPtr->init())
    {
        serverPtr->start();
    }
    else
    {
        cout << "serverPtr init error" << endl;
        PLOGD << "serverPtr init error";
        return -1;
    }
    
    transPtr = CTrans::getInstance();
    if(transPtr->init())
    {
        transPtr->start();
    }
    else
    {
        cout << "transPtr init error" << endl;
        PLOGD << "transPtr init error";
        return -1;
    }

    dataPtr = CData::getInstance();
    if(dataPtr->init())
    {
        dataPtr->start();
    }
    else
    {
        cout << "dataPtr init error" << endl;
        PLOGD << "dataPtr init error";
        return -1;
    }

    while(true)
    {
        sleep(1);
    }

    return 0;
}
